package io.gitee.crud.config.interceptor;

import io.gitee.crud.common.PageContext;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String page = request.getParameter("page");
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.hasText(page)) {
            PageContext.setPage(Integer.parseInt(page));
        }
        if (StringUtils.hasText(pageSize)) {
            PageContext.setPageSize(Integer.parseInt(pageSize));
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        PageContext.clear();
    }
}
